package bb.lanxing.activity.team;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager2.widget.ViewPager2;

import java.util.ArrayList;

import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.adapter.discover.BbsImageEditAdapter;

public class ImageEditActivity extends BaseActivity {
    private TextView titleView;
    private ViewPager2 viewPager;
    private BbsImageEditAdapter bbsImageEditAdapter = new BbsImageEditAdapter();
    private ArrayList<String> imageList = new ArrayList<>();
    private int currentImage = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_edit);

        ImageView back = findViewById(R.id.back);
        back.setOnClickListener(v -> onBackPressed());

        titleView = findViewById(R.id.title);

        ImageView delete = findViewById(R.id.delete);
        delete.setOnClickListener(v -> delete());

        viewPager = findViewById(R.id.viewPager2);
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                currentImage = position;
                titleView.setText((currentImage + 1) + "/" + imageList.size());
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }
        });
        viewPager.setAdapter(bbsImageEditAdapter);

        Intent intent = getIntent();
        imageList = intent.getStringArrayListExtra("images");
        bbsImageEditAdapter.updateData(imageList);

        currentImage = intent.getIntExtra("currentImage", 0);
        titleView.setText((currentImage + 1) + "/" + imageList.size());
        viewPager.setCurrentItem(currentImage, false);
    }

    private void delete() {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("提示").setMessage("要删除这张照片吗？")
                .setPositiveButton("确定", (dialog, id) -> {
                    imageList.remove(currentImage);
                    if (imageList.size() == 0) {
                        setBackResult();
                        finish();
                    }
                    bbsImageEditAdapter.updateData(imageList);
                    if (currentImage == imageList.size()) {
                        currentImage--;
                    }
                    titleView.setText((currentImage + 1) + "/" + imageList.size());
                    viewPager.setCurrentItem(currentImage, false);
                }).setNegativeButton("取消", null);
        AlertDialog dialog = builder.create();
        dialog.show();
    }

    private void setBackResult() {
        Intent intent = new Intent();
        intent.putStringArrayListExtra("images", imageList);
        setResult(RESULT_OK, intent);
    }

    @Override
    public void onBackPressed() {
        setBackResult();
        super.onBackPressed();
    }
}